﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manage.Model;
using Newtonsoft;
using Newtonsoft.Json;

namespace Manage.Basic
{
    public partial class FrmDupartmentMgr : Form
    {
        public FrmDupartmentMgr()
        {
            InitializeComponent();
        }
        private List<SBProvinceModel> sbprList;
        private List<SBCityModel> sbctList;
        private List<SBDistrictModel> sbdtList;
        private bool isSBInit;
        private int Spid;
        private int Scid;
        private int Sdid;
        private void FrmDupartmentMgr_Shown(object sender, EventArgs e)
        {
            try
            {
                //读取税务机关列表
                Dictionary<string, string> dcSBProvince = new Dictionary<string, string>();
                dcSBProvince.Add("Token", Share.MoM.Token);
                RetModel rm = Share.PostData("51API/Company/GetSBProvinceList", dcSBProvince, null);
                if (!rm.Success)
                {
                    MessageBox.Show("读取税务机关列表失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息:" + rm.ErrInfo);
                }
                else
                {
                    SBProvinceModel[] sbpr = JsonConvert.DeserializeObject<SBProvinceModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("省名");
                    for (int i = 0; i < sbpr.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = sbpr[i].Id;
                        dr["省名"] = sbpr[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                    sbprList = sbpr.ToList();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #region 省
        /// <summary>
        /// 添加省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProvince_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                SBProvinceModel spm = new SBProvinceModel();
                spm.Name = tbxProvince.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/SBProvinceAdd", dc, JsonConvert.SerializeObject(spm));
                if (rm.Success)
                {
                    MessageBox.Show("添加省完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加省失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                SBProvinceModel apm = new SBProvinceModel();
                apm.Id = Spid;
                apm.Name = tbxProvince.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/SBProvinceEdit", dc, JsonConvert.SerializeObject(apm));
                if (rm.Success)
                {
                    MessageBox.Show("修改省完成！");

                }
                else
                {
                    MessageBox.Show("修改失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 删除省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Spid.ToString());

                RetModel rm = Share.PostData("51API/Company/SBProvinceDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除省完成！");

                }
                else
                {
                    MessageBox.Show("删除省失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel rm = Share.PostData("51API/Company/GetSBProvinceList", dc, null);
                if (rm.Success)
                {
                    SBProvinceModel[] apm = JsonConvert.DeserializeObject<SBProvinceModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("省名");
                    for (int i = 0; i < apm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = apm[i].Id;
                        dr["省名"] = apm[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询省失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选择省列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
                Spid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
                if (isSBInit)
                    return;
                try
                {
                    isSBInit = true;
                    var obj = Spid;
                    if (0 == obj)
                    {
                        gridControl2.DataSource = null;
                        isSBInit = false;
                        return;
                    }
                    gridControl2.DataSource = null;

                    var res = from x in sbprList where x.Id == Spid select x;
                    SBProvinceModel p = null;
                    if (res.Any())
                    {
                        p = res.ToList()[0];
                    }
                    sbctList = p.Cities.ToList();
                    gridControl2.DataSource = sbctList;
                    isSBInit = false;
                }
                catch (Exception)
                {

                    throw;
                }
            }
            
        #endregion

        #region 市
        /// <summary>
        /// 添加市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton7_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                SBCityModel acm = new SBCityModel();
                acm.Name = textBox1.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/SBCityAdd", dc, JsonConvert.SerializeObject(acm));

                if (rm.Success)
                {
                    MessageBox.Show("添加市成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton6_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaCityModel acm = new AreaCityModel();
                acm.Id = Scid;
                acm.Name = textBox1.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/SBCityEdit", dc, JsonConvert.SerializeObject(acm));
                if (rm.Success)
                {
                    MessageBox.Show("修改市完成!");
                    return;
                }
                else
                {
                    MessageBox.Show("修改市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Scid.ToString());
                RetModel rm = Share.PostData("51API/Company/SBCityDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除市完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("s删除市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("Pid", Spid.ToString());
                RetModel rm = Share.PostData("51API/Company/GetSBCityList", dc, null);
                if (rm.Success)
                {
                    SBCityModel[] apm = JsonConvert.DeserializeObject<SBCityModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("市名");
                    for (int i = 0; i < apm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = apm[i].Id;
                        dr["市名"] = apm[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl2.DataSource = dt;
                    gridView2.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选择市列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Scid = Convert.ToInt32(gridView2.GetRowCellValue(gridView2.GetSelectedRows()[0], gridView2.Columns["id"]));
                isSBInit = true;
                var obj = Spid;
                if (0 == obj)
                {
                    gridControl3.DataSource = null;
                    isSBInit = false;
                    return;
                }
                gridControl3.DataSource = null;

                var res = from x in sbctList where x.Id == Scid select x;
                SBCityModel c = null;
                if (res.Any())
                {
                    c = res.ToList()[0];
                }
                sbdtList = c.Districts.ToList();
                gridControl3.DataSource = sbdtList;
                isSBInit = false;
            }
            catch (Exception)
            {
                isSBInit = false;
                throw;
            }
        }

        #endregion

        #region 县
        /// <summary>
        /// 添加县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton11_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                SBDistrictModel adm = new SBDistrictModel();
                adm.Name = textBox2.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/SBDistrictAdd", dc, JsonConvert.SerializeObject(adm));

                if (rm.Success)
                {
                    MessageBox.Show("添加县/区成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton10_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                SBDistrictModel adm = new SBDistrictModel();
                adm.Id = Sdid;
                adm.Name = textBox2.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/SBDistrictEdit", dc, JsonConvert.SerializeObject(adm));
                if (rm.Success)
                {
                    MessageBox.Show("修改县完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("修改县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton9_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Sdid.ToString());
                RetModel rm = Share.PostData("51API/Company/SBDistrictDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除县完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("删除县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton8_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("Cid", Scid.ToString());
                RetModel rm = Share.PostData("51API/Company/GetDistrictList", dc, null);
                if (rm.Success)
                {
                    SBDistrictModel[] adm = JsonConvert.DeserializeObject<SBDistrictModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("县名");
                    for (int i = 0; i < adm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = adm[i].Id;
                        dr["县名"] = adm[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl3.DataSource = dt;
                    gridView3.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选择县列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView3_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Sdid = Convert.ToInt32(gridView3.GetRowCellValue(gridView3.GetSelectedRows()[0], gridView3.Columns["id"]));
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion


    }
}
